﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using System.Xml.Linq;
namespace ListCopyCOM
{
    /*
     * 
     * 
     * http://stevemannspath.blogspot.com/2013/10/sharepoint-2013-adding-rich-text-column.html
     * Field rtfField = list.Fields.AddFieldAsXml("<Field DisplayName=\"Enhanced RTF\" Type=\"Note\" RestrictedMode=\"TRUE\" RichText=\"TRUE\" RichTextMode=\"FullHtml\" RowOrdinal=\"0\" Required=\"FALSE\" EnforceUniqueValues=\"FALSE\" Indexed=\"FALSE\" NumLines=\"6\" IsolateStyles=\"TRUE\" AppendOnly=\"FALSE\" />"
     * 
     * http://geekswithblogs.net/AnneBougie/archive/2009/03/15/part-2-copy-a-sharepoint-list-programmatically.aspx
     * 
     * 
     * 
     * 
     * 
     */
    class Program
    {
        List<string> theFields = new List<string>();
        static void Main(string[] args)
        {
          
            //Radian 6
            string siteUrl = "http://wyth/sites/demo/";
            string sourceListName = "Announcements";
            string targetListName = "My generic Lists";
            ClientContext clientContext = new ClientContext(siteUrl);

            System.Collections.Generic.List<string> sourceFields = ListFields(clientContext, sourceListName);
            System.Collections.Generic.List<string> targetFields = ListFields(clientContext, targetListName);


            List list = clientContext.Web.Lists.GetByTitle(targetListName);
            clientContext.Load(list.Fields);
            clientContext.ExecuteQuery();

            foreach (var item in sourceFields)
            {
                if (!targetFields.Contains(item))
                {
                    Console.WriteLine(" field to add "+item);
                   string xmlstring= GetFieldDescrAsXML(clientContext, sourceListName, item);
                   XDocument fieldXdoc = XDocument.Parse(xmlstring);
                   fieldXdoc.Descendants().Where(e => e.Name == "ID").Remove();
                   fieldXdoc.Descendants().Where(e => e.Name == "SourceID").Remove(); 
                    AddFieldOptions asdsa = new AddFieldOptions();
                   Field f= list.Fields.AddFieldAsXml(fieldXdoc.ToString(), false, asdsa);
                    f.Update();
                    clientContext.ExecuteQuery();

                }
            }



        }

        private static void CreateList(ClientContext clientContext, string listName)
        {
            Web oWebsite = clientContext.Web;
            ListCreationInformation listCreationInfo = new ListCreationInformation();
            listCreationInfo.Title = listName;
            listCreationInfo.TemplateType = (int)ListTemplateType.GenericList;

            List oList = oWebsite.Lists.Add(listCreationInfo);

            clientContext.ExecuteQuery();
        }

        private static System.Collections.Generic.List<string> ListFields(ClientContext context,string listName)
        {
            List <string> fieldList = new List <string>();
            List list = context.Web.Lists.GetByTitle(listName);
            context.Load(list.Fields);

            // We must call ExecuteQuery before enumerate list.Fields. 
            context.ExecuteQuery();
            
            foreach (Field field in list.Fields  )
            {
                //System.Console.WriteLine(field.InternalName + "  " + field.Title + "   "+ "   "+ field.SchemaXml  +  "    " + field.GetType()   );
                System.Console.WriteLine(field.SchemaXml);
                fieldList.Add(field.InternalName);
               
            }

            return fieldList;
        }


        private static string GetFieldDescrAsXML(ClientContext context, string listName,string field)
        {
            List list = context.Web.Lists.GetByTitle(listName);
            context.Load(list.Fields);
            context.ExecuteQuery();
            foreach (Field column in list.Fields)
            {
                if (column.InternalName.Equals(field))
                {
                    return column.SchemaXml;
                }
            }
            return "";
        }
    }
}